/**
 * 类型为ans字段处理
 * ASCII字符，数组，特殊字符处理
 * 中文使用GB18030
 * 转到内部使用系统默认编码
 */
package cc.rengu.igas.channel.cups.common.util;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.jradp.exception.SystemException;
import cc.rengu.jradp.mods.trmsg.iso8583.FieldFactory;
import cc.rengu.jradp.mods.trmsg.iso8583.IsoTypeFactory;
import cc.rengu.jradp.mods.trmsg.iso8583.type.length.NonLengthTypeParseInfo;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author lizhangtao
 */
public class AlphaNumericSpecialFieldParseInfo extends IsoTypeFactory {

    @Override
    /**
     * ASCII格式，无需decode
     */
    public FieldFactory decode(byte[] buf, int pos, Map<String, FieldFactory> fields, String index) {
        FieldFactory field = fields.get(index);
        if (pos < 0) {
            throw new SystemException("字母，数字，特殊字符解析,位置非法pos=[" + pos + "]");
        } else if (pos + field.getLength() > buf.length) {
            throw new SystemException("字母，数字，特殊字符解析,位置非法pos=[" + pos + "] + "
                    + "[" + field.getLength() + "] > message length[" + buf.length + "]");

        } else {
            String value;
            try {
                value = new String(buf, pos, field.getLength(), CupsParamConstant.GB18030);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new SystemException("不支持的编码格式" + CupsParamConstant.GB18030);
            }

            byte[] dest = new byte[field.getLength()];
            System.arraycopy(buf, pos, dest, 0, field.getLength());
            field.setOrgValue(dest);
            field.setValue(value);
            return field;
        }
    }

    @Override
    /**
     * 默认不检查
     */
    public boolean check(byte[] buf, int pos, Map<String, FieldFactory> fields, String index) {

        /**
         * 字母，数字，特殊字符。因此不检查
         */
        return true;
    }

    @Override
    public FieldFactory encode(Map<String, FieldFactory> fields, String index) {
        FieldFactory field = fields.get(index);
        if (field.getLenType() instanceof NonLengthTypeParseInfo) {
            String value = field.getValue();
            byte[] bt = null;
            try {
                bt = value.getBytes(CupsParamConstant.GB18030);
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
                throw new SystemException("不支持的编码格式GB18030");
            }
            int length = field.getLength();
            byte[] byteTmp = null;
            try {
                byteTmp = String.format("%-" + length + "s", " ").getBytes(CupsParamConstant.GB18030);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new SystemException("不支持的编码格式" + CupsParamConstant.GB18030);
            }
            System.arraycopy(bt, 0, byteTmp, 0, Math.min(bt.length, length));
            byte[] out = MacFieldProcUtil.macProc(byteTmp);
            field.setOrgValue(out);
        } else {
            byte[] byteTmp;
            try {
                byteTmp = fields.get(index).getValue().getBytes(CupsParamConstant.GB18030);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new SystemException("不支持的编码格式" + CupsParamConstant.GB18030);
            }

            field.setLength(byteTmp.length);

            byte[] out = MacFieldProcUtil.macProc(byteTmp);
            field.setOrgValue(out);
        }
        return field;
    }
}
